<?php
/**
 * Natural Theme Patch
 *
 * @description Adds a bunch of automation functionality to Natural Theme
 * @author Mark Anthony Degamo
 * @version 1.0
 */

    class NaturalExtension {
        private $utils,
                $default_actions;

        public function __construct() {
            require_once(OF_FILEPATH . '/patch/inc/Utils.php');

            $this->utils = new NaturalExtension_Utils();
        }

        public function load_admin_interface() {
            $curr_page = isset($_GET['page']) ? $_GET['page'] : '';

            // load the required files
            $this->utils->load_file('inc', 'PageBuilder');
            $this->utils->load_file('inc', 'SMMBuilder');
            $this->utils->load_file('inc', 'Bookshelf');
            $this->utils->load_file('inc', 'Shortcodes');
            $this->utils->load_file('inc', 'Settings');
            $this->utils->load_file('inc', 'DefaultActions');

            // load the ajax handlers
            $this->utils->load_file('ajax', 'PageBuilder');
            $this->utils->load_file('ajax', 'SMMBuilder');
            $this->utils->load_file('ajax', 'Bookshelf');

            // late initializers
            $this->default_actions = new NaturalExtension_DefaultActions();

            // tweak the excerpt behavior
            add_filter('excerpt_more', array($this, 'custom_excerpt_more'));
            add_filter('excerpt_length', array($this, 'custom_excerpt_length'));

            // tweak the page template "Single page with right sidebar" for books page
            add_filter('the_content', array($this, 'update_book_page_content'));

            // tweak the footer text
            add_filter('update_footer', array($this, 'custom_footer_version_text'), 9999);

            // show some messages
            add_action('admin_notices', array($this->default_actions, 'show_admin_messages'));

            // hook the menus to admin_menu
            add_action('admin_menu', array($this, 'register_admin_menu'));

            // load admin styles and scripts for Natural Extension pages only
            if (preg_match('/^natext/', $curr_page)  == '1') {
                // hook styles and scripts to admin_head
                add_action('admin_head', array($this, 'register_styles_and_scripts'));
            }
        }


        // Registrars
        // ------------------------------

        public function register_styles_and_scripts() {
            if ($this->utils->config['devil_mode'] == 'true') {
                echo '<link rel="stylesheet/less" href="' . $this->utils->get_path('less', 'patch') . '">';
            } else {
                $this->utils->nq_styles(
                    array('patch-styles', 'patch')
                );
            }

            // default scripts
            $this->utils->nq_scripts(
                array('underscore', 'underscore', '', '1.3.3'),
                array('amplify', 'amplify', array('jquery'), '1.1.0'),
                array('natext', 'natext', array('jquery', 'underscore'), $this->utils->config['version']),
                array('less', 'less', '', '1.3.0')
            );

            // add some global js objects
            wp_localize_script(
                'natext',
                'NatExt',
                array(
                    'wp' => array(
                        'admin_url'         => admin_url('admin.php'),
                        'ajax_url'          => admin_url('admin-ajax.php'),
                        'theme_url'         => get_template_directory_uri(),
                        'media_upload_url'  => admin_url('media-upload.php'),
                        'install_url'       => get_bloginfo('wpurl')
                    )
                )
            );
        }

        public function register_admin_menu() {
            // add the base menu
            add_menu_page(
                'Natural Theme Extension',
                'Natural Ext',
                'add_users',
                'natext',
                array($this, 'page_Natural_Extension'),
                $this->utils->get_path('img', 'ico_menu')
            );

            // add the submenus after service initialization
            if ($this->utils->get_option('service_chosen') == '1') {
                // smm builder
                add_submenu_page(
                    'natext',
                    'Social Media Link Builder',
                    'SMM Builder',
                    'add_users',
                    'natext-smm-builder',
                    array($this, 'page_SMM_Builder')
                );

                // bookshelf
                add_submenu_page(
                    'natext',
                    'Bookshelf',
                    'Bookshelf',
                    'add_users',
                    'natext-bookshelf',
                    array($this, 'page_Bookshelf')
                );
            }
        }

        public function custom_excerpt_more() {
            return '.....';
        }

        public function custom_excerpt_length() {
            return 80;
        }

        public function custom_footer_version_text($default_text) {
            return 'WordPress ' . $default_text . ' &nbsp;&nbsp;|&nbsp;&nbsp;  Patch Version ' . $this->utils->config['version'];
        }

        public function update_book_page_content($content) {
            global $post;

            $book_page_id = $this->utils->get_page_id_via_name('book');

            if (is_page_template('page-right-sidebar.php')) {
                if (($post->post_parent == $book_page_id) && (! is_page('book'))) {
                    $thumb  = do_shortcode('[natext_book_image force_display="true"]');
                    $button = do_shortcode('[natext_book_order_button force_display="true"]');

                    return $thumb . $content . $button;
                }
            }

            return $content;
        }

        // callbacks for Pages
        // ------------------------------

        public function page_Natural_Extension() {
            if ($this->utils->get_option('service_chosen') == '1') {
                get_template_part('/patch/inc/view', 'settings');
            } else {
                get_template_part('/patch/inc/view', 'page-builder');
            }
        }

        public function page_SMM_Builder() {
            get_template_part('/patch/inc/view', 'smm-builder');
        }

        public function page_Bookshelf() {
            get_template_part('/patch/inc/view', 'bookshelf');
        }
    }


    // instantiate NaturalExtension
    $natural_extension = new NaturalExtension();

    $natural_extension->load_admin_interface();
?>